package cn.maidouya.org.service.impl;

import cn.maidouya.basic.service.impl.BaseServiceImpl;
import cn.maidouya.basic.util.PageLIst;
import cn.maidouya.org.domain.Department;
import cn.maidouya.org.mapper.DepartmentMapper;
import cn.maidouya.org.mapper.EmployeeMapper;
import cn.maidouya.org.query.DepartmentQuery;
import cn.maidouya.org.service.DepartmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
@Service
public class DepartmentServiceImpl extends BaseServiceImpl<Department> implements DepartmentService {
    @Autowired
    private DepartmentMapper departmentMapper;
    @Override
    @Transactional
    public void save(Department department) {
        /**
         *      第一步先添加数据并获取当前添加的主键id
         *      第二步 判断parent是否为空 如果为null那么我就把自己的id给到dirPath
         *      第三步 判断平parent有值的情况  就parent上下级id给到dirPath赋值
         */
        departmentMapper.save(department);
        if (department.getParent() == null){
            // 判断上级部门为空时  就把id的值修改给dirPath字段
            department.setDirPath("/"+ department.getId());
        }else {
            /**
             *     1.获取父路径数据id
             *     2. 查询一个添加部门所在的id
             *     3. 通过部门id获取dirPath的字段值
             *     4. 拼接部门id家dirPath到达dirPath中去
             *     5. 调用方法修改到数据库
             * */
            // 获取当前添加数据上下级值并获取的他的id
            Long part_id = department.getParent().getId();// 获取添加的id
            // 调用方法
            Department part_dept = departmentMapper.loadById(part_id);
            String dirPath = part_dept.getDirPath();
            department.setDirPath(dirPath+"/"+department.getId());
        }
        departmentMapper.update(department);
    }

    @Override
    @Transactional
    public void update(Department department) {
        if (department.getParent().getId() == null){// 判断parent部门id是否为空   是空
            //  为空则获取获取员工或者经理存在的id修改到dirPath中去
           department.setDirPath("/"+department.getId());
        }
        if (department.getParent().getId() != null){// 判断parent部门id是否为空   不为空
            // 简写模式
            String dirPath = departmentMapper.loadById(department.getParent().getId()).getDirPath();
            department.setDirPath(dirPath+"/" +department.getId());
            // 流程模式
          /*  // 获取到部门id
            Long newParent = department.getParent().getId();
            // 通过部门查询部门的对象
            Department findParent = departmentMapper.findOne(newParent);// parent对象
            // 找到了部门id和上级部门id拼接一起：/2/5
            String parentNwDirpath = findParent.getDirPath();
            // 部门给自己的id   /96
            Long mydeptId = department.getId();
            // 拼接上级部门和所有部门、自己id    拼接字符串
           String newDirpath =  parentNwDirpath +"/"+ mydeptId;
            // 调用方法修改到数据库中去
            department.setDirPath(newDirpath);*/
        }


        departmentMapper.update(department);
    }
    // 批量删除
    @Override
    @Transactional
    public void batDel(Long[] ids) {
        departmentMapper.batDel(ids);
    }

    // 查询上级部门
    @Override
    public List<Department> deptTree() {
        List<Department> list = departmentMapper.deptTree();
        return list;
    }
}
